#!/bin/bash

set -e

mkdir -p channel-artifacts
mkdir -p crypto-config
rm -fr channel-artifacts/*
rm -fr crypto-config/*

CHANNEL_NAME=mychannel
CHAINCODE_NAME=mychaincode
COMPOSE_FILES=docker-compose-cli.yaml

echo "1. 生成认证证书"
cryptogen generate --config=./crypto-config.yaml

echo "2. 生成起始区块"
configtxgen -profile TwoOrgsOrdererGenesis -channelID system-channel -outputBlock ./channel-artifacts/genesis.block

echo "3. 生成通道配置文件"
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME

echo "4. 生成锚节点配置文件"
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP

echo "5. 启动容器"
docker-compose -f ${COMPOSE_FILES} up -d

echo "6. 创建通道"
docker exec cli peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f /opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts/channel.tx

echo "7. 将节点加入通道"
docker exec cli peer channel join -b $CHANNEL_NAME.block
sleep 3
docker exec -e CORE_PEER_ADDRESS=peer1.org1.example.com:8051 cli peer channel join -b $CHANNEL_NAME.block
sleep 3
docker exec -e CORE_PEER_LOCALMSPID="Org2MSP" -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp -e CORE_PEER_ADDRESS=peer0.org2.example.com:9051 cli peer channel join -b $CHANNEL_NAME.block
sleep 3
docker exec -e CORE_PEER_LOCALMSPID="Org2MSP" -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp -e CORE_PEER_ADDRESS=peer1.org2.example.com:10051 cli peer channel join -b $CHANNEL_NAME.block
sleep 3
echo "8. 更新锚节点"
docker exec cli peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx
docker exec -e CORE_PEER_LOCALMSPID="Org2MSP" -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp -e CORE_PEER_ADDRESS=peer0.org2.example.com:9051 cli peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx

echo "9. 安装链码"
docker exec cli peer chaincode install -n $CHAINCODE_NAME -v 1.0 -l golang -p github.com/chaincode/finanice
docker exec -e CORE_PEER_LOCALMSPID="Org2MSP" -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp -e CORE_PEER_ADDRESS=peer0.org2.example.com:9051 cli peer chaincode install -n $CHAINCODE_NAME -v 1.0 -l golang -p github.com/chaincode/finanice

echo "10. 实例化链码"
docker exec cli peer chaincode instantiate -o orderer.example.com:7050 -C $CHANNEL_NAME -n $CHAINCODE_NAME -l golang -v 1.0 -c '{"Args":["init"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')"

sleep 5
echo "11. 通过链码查询实例化第二条链码"
docker exec -e CORE_PEER_LOCALMSPID="Org2MSP" -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp -e CORE_PEER_ADDRESS=peer0.org2.example.com:9051 cli peer chaincode query -C $CHANNEL_NAME -n $CHAINCODE_NAME -c '{"Args":["getCompany","company"]}'
#
#sleep 5
#echo "12. 调用invoke"
#docker exec cli peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n $CHAINCODE_NAME --peerAddresses peer0.org1.example.com:7051 -c '{"Args":["createIDCard","{\"id\":\"1003\",\"name\":\"刘六\",\"age\":23,\"sex\":\"男\"}"]}'
#
#echo "13. 在peer1.org1节点上安装链码"
#docker exec -e CORE_PEER_ADDRESS=peer1.org1.example.com:8051 cli peer chaincode install -n $CHAINCODE_NAME -v 1.0 -l golang -p github.com/chaincode/bank
#
#echo "14. 查询链码 peer1.org1"
#docker exec -e CORE_PEER_ADDRESS=peer1.org1.example.com:8051 cli peer chaincode query -C $CHANNEL_NAME -n $CHAINCODE_NAME -c '{"Args":["queryBank","654321654321654321","123456"]}'
#
#echo "15. 在peer1.org2节点上安装链链码"
#docker exec -e CORE_PEER_LOCALMSPID="Org2MSP" -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp -e CORE_PEER_ADDRESS=peer1.org2.example.com:10051 cli peer chaincode install -n $CHAINCODE_NAME -v 1.0 -l golang -p github.com/chaincode/bank
#
#echo "16. 查询链码 peer1.org2"
#docker exec -e CORE_PEER_LOCALMSPID="Org2MSP" -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp -e CORE_PEER_ADDRESS=peer1.org2.example.com:10051 cli peer chaincode query -C $CHANNEL_NAME -n $CHAINCODE_NAME -c '{"Args":["queryBank","654321654321654321","123456"]}'
